﻿using System;
using System.Collections.Generic;

using System.Web;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using System.Web.Security;
using System.Diagnostics;
using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Text;
using System.Configuration;
using System.IO;
using System.Collections;
using System.Data.SqlClient;


/// <summary>
/// Summary description for OrderMaterial
/// </summary>
public class OrderMaterial
{
    public int MaterialID;

    public int Count;

    public OrderMaterial(int mID,int icount)
    {
        //
        // TODO: Add constructor logic here
        //
        MaterialID = mID;
        Count = icount;
    }

}
public class MRPUtility
{


    public static string buildDateCondition(string sFieldName, string sStartDate, string sEndDate)
    {
        string sWhere = string.Empty;
        if (string.IsNullOrEmpty(sStartDate) && (!string.IsNullOrEmpty(sEndDate)))
        {
            sWhere = string.Format("T.{1}<='{0}'", sEndDate, sFieldName);
        }
        else if (!string.IsNullOrEmpty(sStartDate) && (string.IsNullOrEmpty(sEndDate)))
        {
            sWhere = string.Format("T.{1}>='{0}'", sStartDate, sFieldName);
        }
        else if (!string.IsNullOrEmpty(sStartDate) && (!string.IsNullOrEmpty(sEndDate)))
        {
            sWhere = string.Format("T.{2} between '{0}' and '{1}'", sStartDate, sEndDate, sFieldName);
        }
        return sWhere;
    }

    /// <summary>
    /// 写web.config
    /// </summary>
    /// <param name="item"></param>
    /// <param name="key"></param>
    /// <param name="value"></param>
    public static void writeConfig(string item, string key, string value)
    {
        if (item == "")
        {
            item = "appSettings";
        }
        Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(System.Web.HttpContext.Current.Request.ApplicationPath);
        AppSettingsSection appSection = (AppSettingsSection)config.GetSection(item);
        if (appSection.Settings[key] == null)
        {
            appSection.Settings.Add(key, value);
            config.Save();
        }
        else
        {
            appSection.Settings.Remove(key);
            appSection.Settings.Add(key, value);
            config.Save();
        }
    }
    public static void WriteLog(string msg, string Categorie)
    {
        WriteLog(msg, Categorie, TraceEventType.Information);
        
    }
    public static void WriteLog(string msg, string Categorie, TraceEventType Severity)
    {
        //创建一个日志条目
        LogEntry log = new LogEntry();
        //指定该日志所属类别
        log.Categories.Add(Categorie);
        //日志标题
        if (Membership.GetUser() != null)
            log.Title = Membership.GetUser().UserName;
        else
            log.Title = "System";
        log.Message = msg;
        //优先级
        log.Priority = 0;
        log.Severity = Severity;

        try
        {
            Logger.Write(log);
        }
        catch (Exception ex)
        {
            //TODO:....
        }
    }
    public static void WriteGeneralLog(string msg)
    {
        WriteLog(msg, "General");
    }
    public static void WriteLogForSQL(string sql)
    {
        WriteLog(sql, "SQLScript");
    }
    public static void WriteErrorLog(string sql)
    {
        WriteLog(sql, "Error",TraceEventType.Error);
    }

    public static void WriteLogByUser(string msg, string Categorie)
    {
        if (Membership.GetUser() != null)
            return;
        else
            WriteLog(msg, Categorie);
    }

    /// <summary>
    /// 从字符串中分离出某个字段的值；
    /// 目前
    /// </summary>
    /// <param name="fieldName"></param>
    /// <param name="oldValues"></param>
    /// <param name="noRecValue"></param>
    /// <returns></returns>
    public static string GetOldValueFromString(string fieldName, Dictionary<string, string> oldValues, string noRecValue)
    {
        string ret=string.Empty;

        if(oldValues.ContainsKey(fieldName))
            ret=oldValues[fieldName];

        if (string.IsNullOrEmpty(ret))
            ret=noRecValue;
        return ret;
    }

    public static string GenText(string displayText, string sql, string noRecMsg)
    {
        string sRet = displayText;
        int columnCount = 0;
        StringBuilder sb = new StringBuilder();
        if (!String.IsNullOrEmpty(sql))
        {

            Database db = DatabaseFactory.CreateDatabase("MRPSqlServer");
            DataSet ds = db.ExecuteDataSet(CommandType.Text, sql);

            if (ds.Tables.Count > 0)
            {
                if (ds.Tables[0].Rows.Count == 1)
                {
                    columnCount = ds.Tables[0].Columns.Count;
                    DataRow dr = ds.Tables[0].Rows[0];
                    displayText = "<div class=\"quicklinktext\">" + displayText + "</div>";
                    switch (columnCount)
                    {
                        case 1:
                            sRet = string.Format(displayText, dr.ItemArray[0]);
                            break;
                        case 2:
                            sRet = string.Format(displayText, dr.ItemArray[0], dr.ItemArray[1]);
                            break;
                        case 3:
                            sRet = string.Format(displayText, dr.ItemArray[0], dr.ItemArray[1], dr.ItemArray[2]);
                            break;
                        case 4:
                            sRet = string.Format(displayText, dr.ItemArray[0], dr.ItemArray[1], dr.ItemArray[2], dr.ItemArray[3]);
                            break;
                        case 5:
                            sRet = string.Format(displayText, dr.ItemArray[0], dr.ItemArray[1], dr.ItemArray[2], dr.ItemArray[3], dr.ItemArray[4]);
                            break;
                        case 6:
                            sRet = string.Format(displayText, dr.ItemArray[0], dr.ItemArray[1], dr.ItemArray[2], dr.ItemArray[3], dr.ItemArray[4], dr.ItemArray[5]);
                            break;
                        default:
                            sRet = displayText;
                            
                            break;
                    }
                    columnCount = 0;
                }
                else if(ds.Tables[0].Rows.Count>1)
                {
                    columnCount = ds.Tables[0].Columns.Count;
                    sb.Append("<div class=\"homepagerow\"><ul>");
                    string fs ="<li>"+ displayText+"</li>";

                    foreach(DataRow dr in ds.Tables[0].Rows)
                    {
                        switch (columnCount)
                        {
                            case 1:
                                sb.AppendFormat(fs, dr.ItemArray[0]);
                                break;
                            case 2:
                                sb.AppendFormat(fs, dr.ItemArray[0], dr.ItemArray[1]);
                                break;
                            case 3:
                                sb.AppendFormat(fs, dr.ItemArray[0], dr.ItemArray[1], dr.ItemArray[2]);
                                break;
                            case 4:
                                sb.AppendFormat(fs, dr.ItemArray[0], dr.ItemArray[1], dr.ItemArray[2], dr.ItemArray[3]);
                                break;
                            case 5:
                                sb.AppendFormat(fs, dr.ItemArray[0], dr.ItemArray[1], dr.ItemArray[2], dr.ItemArray[3], dr.ItemArray[4]);
                                break;
                            case 6:
                                sb.AppendFormat(fs, dr.ItemArray[0], dr.ItemArray[1], dr.ItemArray[2], dr.ItemArray[3], dr.ItemArray[4], dr.ItemArray[5]);
                                break;
                            default:
                                sRet = displayText;
                                columnCount = 0;
                                break;
                        }
                    }
                    sb.Append("</ul></div>");
                }
            }
            else
                sRet = noRecMsg;
        }
        if (columnCount > 0)
        {
            sRet = sb.ToString();
        }
        return sRet;

    }

    

}
